/*
 *  Copyright (C) 2009 takaji
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * AccountDetailPanel.java
 *
 * Created on Dec 24, 2009, 5:09:08 PM
 */
package dakside.hacc.modules.accounting.accounttree;

import dakside.hacc.core.models.Account;
import dakside.hacc.core.models.Currency;
import org.dakside.duck.helpers.SwingHelper;
import org.dakside.utils.FinancialHelper;
import javax.swing.ComboBoxModel;

/**
 * Account details panel
 * @author Takaji
 */
public class AccountDetailPanel extends javax.swing.JPanel {

    /** Creates new form AccountDetailPanel */
    public AccountDetailPanel() {
        initComponents();
        refresh();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txtCode = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        cboType = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        cboCurrency = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        txtAccountBalance = new javax.swing.JTextField();

        jLabel1.setText("Account Code");

        jLabel2.setText("Account Name");

        jLabel3.setText("Account Type");

        cboType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Debit", "Credit", "Folder" }));

        jLabel4.setText("Base currency");

        cboCurrency.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel5.setText("Balance");

        txtAccountBalance.setEditable(false);
        txtAccountBalance.setText("0");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3))
                        .addGap(9, 9, 9)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtName, javax.swing.GroupLayout.DEFAULT_SIZE, 309, Short.MAX_VALUE)
                            .addComponent(cboType, javax.swing.GroupLayout.Alignment.TRAILING, 0, 309, Short.MAX_VALUE)
                            .addComponent(txtCode, javax.swing.GroupLayout.DEFAULT_SIZE, 309, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtAccountBalance, javax.swing.GroupLayout.DEFAULT_SIZE, 308, Short.MAX_VALUE)
                            .addComponent(cboCurrency, 0, 308, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtCode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cboType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(cboCurrency, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtAccountBalance, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cboCurrency;
    private javax.swing.JComboBox cboType;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JTextField txtAccountBalance;
    private javax.swing.JTextField txtCode;
    private javax.swing.JTextField txtName;
    // End of variables declaration//GEN-END:variables
    Account currentAccount = null;

    //user code
    public Account getAccount() {
        if (currentAccount == null) {
            currentAccount = new Account();
        }
        currentAccount.setCode(txtCode.getText());
        currentAccount.setName(txtName.getText());

        //process selected item
        System.out.println((String) cboType.getSelectedItem());
        try {
            if ("Debit".equals((String) cboType.getSelectedItem())) {
                currentAccount.setType(Account.TYPE_DEBIT);
            } else if ("Credit".equals((String) cboType.getSelectedItem())) {
                currentAccount.setType(Account.TYPE_CREDIT);
            } else if ("Folder".equals((String) cboType.getSelectedItem())) {
                currentAccount.setType(Account.TYPE_FOLDER);
            }
        } catch (Exception ex) {
            //nevermind the exception here :P
        }
        currentAccount.setCurrency((Currency) cboCurrency.getSelectedItem());

        return currentAccount;
    }

    /**
     * Display account information on panel
     * @param account
     */
    public void setAccount(Account account) {
        //save account
        this.currentAccount = (account == null) ? new Account() : account;

        txtCode.setText(currentAccount.getCode());
        txtName.setText(currentAccount.getName());
        //load account type
        ComboBoxModel model = cboType.getModel();
        for (int i = 0; i < model.getSize(); i++) {
            if (model.getElementAt(i).toString().equals(currentAccount.getTypeString())) {
                cboType.setSelectedIndex(i);
                break;
            }
        }
        if (currentAccount.getCurrency() != null) {
            cboCurrency.setSelectedItem(currentAccount.getCurrency());
        }

        //recalculate account balance
        if (currentAccount.getType() != Account.TYPE_FOLDER) {
            try {
                double balance = AccountManager.getInstance().getAccountBalance(currentAccount);
                txtAccountBalance.setText(FinancialHelper.formatCurrency(balance));
            } catch (Exception ex) {
            }
        } else {
            txtAccountBalance.setText("");
        }
        //end of set account
    }

    void refresh() {
        Object previousSelectedItem =
                cboCurrency.getSelectedItem() instanceof Currency
                ? cboCurrency.getSelectedItem() : AccountManager.getInstance().getDefaultCurrency();
        Currency[] currencies = AccountManager.getInstance().getCurrencies();
        SwingHelper.initCombobox(cboCurrency, currencies, previousSelectedItem);
    }

    /**
     * Account info only can be changed if there's no posting has been done on the account
     */
    void setState(boolean canBeChanged) {
        txtCode.setEnabled(canBeChanged);
        cboCurrency.setEnabled(canBeChanged);
        cboType.setEnabled(canBeChanged);
    }
}
